
import util from "../../../common/js/util.js";
import Qs from "qs";

var app = getApp();
const pageSize = 10;
Page({
  data: {
    dataList: [],
    readNum: 0,
    page: 1,
    pageSize: pageSize,
    showLoadMore: false,
    showLoadMoreTxt: "over",
    totalResult: pageSize,
    totalnum: 0,
  },
  onLoad() {
    this.setData({
      showLoadMoreTxt: "load"
    });
    // this.tableList();
    dd.onComponentError(this.callBack);
  },
  onShow() {
    // 页面显示
    this.searchFn();
  },
  onPullDownRefresh() {
    this.searchFn();
  },
  tabClick(e) {
    let status = e.currentTarget.dataset.status;
    this.setData({
      status,
      totalnum: 0,
      dataList: [],
      page: 1,
      totalResult: pageSize,
    });
    this.tableList();
  },
 
  onRoute(e) {
    let VIEWS_ID = e.currentTarget.dataset.id;
    let obj = {};
    for (let index = 0; index < this.data.dataList.length; index++) {
      const element = this.data.dataList[index];
      if (VIEWS_ID == element.VIEWS_ID) {
        obj = element;
        break;
      }
    }
  },
  tableList(obj){
    let api = "/user/book/noteList";
    let list = {
      page: this.data.page,
      pageSize: this.data.pageSize,
      bookId: this.data.queryData.bookId,
      userId: this.data.queryData.userId,
      chapter: this.data.queryData.value
    };
    if(obj){
      list=util.mergeObj(list,obj)
    }
    console.log("--list:",list);
    let that = this;
    this.setData({
      showLoadMoreTxt: "load"
    });
    
    // console.log(this.data.totalnum,this.data.totalResult);
    //debugger
    if (this.data.dataList.length >= this.data.totalResult&&this.data.totalResult!=0) {
      this.setData({
        showLoadMoreTxt: "over"
      })
      dd.stopPullDownRefresh();
      return false;
    }
    dd.showLoading({
        content: '数据加载中...',
      })
    let dataList=this.data.recordAudioArr;
    app.$post(api, list).then(res => {
      dd.stopPullDownRefresh()
     dd.hideLoading();
      if (res.code == 200) {

        let arr_list=dataList.concat(res.data.list);
        let page=this.data.page;
        page=page+1;
        
        this.setData({
          totalResult: res.data.totalResult,
          page,
          recordAudioArr: arr_list,
          showLoadMoreTxt: "over"
        });
        
      }else{
        util.message(res.message);

      }
    })
  },
  onPullDownRefresh() {
    // 页面被下拉
    this.setData({
      showLoadMoreTxt: "load"
    });
    this.searchFn();
  },
  onScrollToLower() {
    this.setData({
      showLoadMoreTxt: "load"
    });
    this.tableList();
  },
  searchFn() {
    this.setData({
      currentPage: 1,
      totalnum: 0,
      dataList: []
    });
    this.tableList()
  },

  tableList() {

    let api = "/YhERP/datatool/IndexPendingTrial", 
    list = { 
      currentPage: this.data.currentPage, 
      showCount: this.data.showCount 
      
    };
    list[`${this.data.status}`] = app.prototype.$SysUserName;
    let that = this;
    this.setData({
      showLoadMoreTxt: "load"
    });
    if (this.data.totalnum >= this.data.totalResult) {
      this.setData({
        showLoadMoreTxt: "over"
      })
      dd.stopPullDownRefresh();
      return false;
    }
    dd.showLoading({
        content: '数据加载中...',
      })
    app.$post(api, list).then(res => {
      if (res.is == 1) {
        res.data.dataList.forEach(e => {
          let json = this.parseStatusName(e);
          e.STATUS_NAME = json.msg;
          e.status_obj = json;
          e.type_json = this.tableListTypeName(e);
          //console.log("---type_json:",e.type_json);   
          e.TYPE_NAMES = e.type_json.type_name;
          
          let CC_LIST = e.APPROVAL_NOW.split(",");
          let APPROVAL_NOW_NAME = [];
          for (let i in CC_LIST) {
            APPROVAL_NOW_NAME.push(app.prototype.$USERNAME_MAP.get(CC_LIST[i]));
          }
          e.APPROVAL_NOW_NAME = APPROVAL_NOW_NAME.join(",");
          // console.log("-----e:",e);
        });

        this.setData({
          totalResult: res.data.totalResult,
          totalnum: that.data.currentPage * that.data.showCount,
          currentPage: that.data.currentPage + 1,
          dataList: that.data.dataList.concat(res.data.dataList),
          showLoadMoreTxt: "over"
        });
        if (this.data.status == "APPROVAL_NOW") {
          this.setData({
            APPROVAL_NOW_TOTAL: res.data.totalResult
          })
        }
        dd.hideLoading();
        dd.stopPullDownRefresh();
      }
    }).catch(e=>{
       console.log("---捕获异常:",e);
    })

  },
  onReachBottom() {
    this.setData({
      showLoadMoreTxt: "load"
    });
    // this.setData({
    //   showLoadMore: true
    // })
    this.tableList();
  },
  callBack(error, method, component) {
    dd.alert({
      title: "监听自定义组件内的error异常",
      content: JSON.stringify(error) + JSON.stringify(method) + JSON.stringify(component),
    });
  },
});
